package com.hpjpw.idoapp.controller;

import com.hpjpw.ido.domain.AnonymousMoment;
import com.hpjpw.ido.service.IAnonymousMomentService;
import com.hpjpw.idoapp.service.IDoAnonymousService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/ido/app/anonymousMoment")
public class IDoAnonymousMomentController extends BaseController {
    @Autowired
    IAnonymousMomentService anonymousMomentService;
    @Autowired
    IDoAnonymousService anonymousService;
    @Autowired
    ISysUserService sysUserService;

    /**
     * 查询撕逼墙消息列表
     */
    @ApiOperation("查询撕逼墙消息列表，可带分页参数：pageNum=1&pageSize=10")
    @GetMapping("/list")
    public TableDataInfo list(){
        startPage();
        List<Map<String, Object>> mapList = anonymousService.momentListReSponse();
        return getDataTable(mapList);
    }

    /**
     * 新增撕逼墙消息
     */
    @ApiOperation("新增撕逼墙消息，接收content(动态内容),picture(图片，最多三张)")
    @PostMapping()
    public AjaxResult addMoment(@RequestBody AnonymousMoment anonymousMoment) {
        /* 安全校验1，无动态内容无图片 */
        if (StringUtils.isNull(anonymousMoment.getContent())&&StringUtils.isNull(anonymousMoment.getPicture1())&&StringUtils.isNull(anonymousMoment.getPicture2())&&StringUtils.isNull(anonymousMoment.getPicture3())){
            return AjaxResult.error("错误！没有动态内容和图片！");
        }
        AnonymousMoment anonymousMomentAdd = new AnonymousMoment();
        /* 只接受来自前端的固定内容 */
        Long userId = SecurityUtils.getUserId();
        anonymousMomentAdd.setReportId(userId);
        anonymousMomentAdd.setContent(anonymousMoment.getContent());
        anonymousMomentAdd.setPicture1(anonymousMoment.getPicture1());
        anonymousMomentAdd.setPicture2(anonymousMoment.getPicture2());
        anonymousMomentAdd.setPicture3(anonymousMoment.getPicture3());
        anonymousMomentAdd.setTime(new Date());
        anonymousMomentService.insertAnonymousMoment(anonymousMomentAdd);
        return AjaxResult.success();
    }

    /**
     * 查询撕逼墙动态
     */
    @ApiOperation("查询撕逼墙动态，接收参数moment_id")
    @GetMapping("/{momentId}")
    public TableDataInfo query(@PathVariable("momentId") long momentId){
        List<Map<String, Object>> mapList = new ArrayList<>();
        Map<String, Object> map = anonymousService.momentResponse(momentId);
        mapList.add(map);
        return getDataTable(mapList);
    }

    /**
     * 删除撕逼墙动态
     */
    @ApiOperation("删除撕逼墙动态，接收id(动态id)")
    @DeleteMapping()
    public AjaxResult deleteMoment(@RequestBody AnonymousMoment anonymousMoment) {
        AnonymousMoment anonymousMomentCheck = anonymousMomentService.selectAnonymousMomentById(anonymousMoment.getId());
        /* 安全校验1，无此动态 */
        if (StringUtils.isNull(anonymousMomentCheck)){
            return AjaxResult.error("错误！无此动态，请检查id");
        }
        /* 安全校验2，删除操作者非动态发布者 */
        Long userId = SecurityUtils.getUserId();
        if (!(anonymousMomentCheck.getReportId().equals(userId))){
            return AjaxResult.error("错误！没有权限无法删除");
        }
        anonymousMomentService.deleteAnonymousMomentById(anonymousMomentCheck.getId());
        return AjaxResult.success();
    }
    //TODO:删除动态时级联删除评论和点赞
}
